package com.wyp.didishige;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


import com.wyp.didishige.activity.MainActivity;
import com.wyp.didishige.activity.dynamic.DynamicDetailActivity;
import com.wyp.didishige.activity.im.ChatActivity;
import com.wyp.didishige.bean.AddFriendMessage;
import com.wyp.didishige.bean.AgreeAddFriendMessage;
import com.wyp.didishige.bean.SendCommentMessage;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.config.Constants;
import com.wyp.didishige.db.BlackUserManager;
import com.wyp.didishige.db.NewComment;
import com.wyp.didishige.db.NewCommentManager;
import com.wyp.didishige.db.NewFriend;
import com.wyp.didishige.db.NewFriendManager;
import com.wyp.didishige.event.RefreshEvent;
import com.wyp.didishige.model.User;
import com.wyp.didishige.model.UserModel;
import com.wyp.didishige.model.i.UpdateCacheListener;
import com.wyp.didishige.utils.Util;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

import cn.bmob.newim.BmobIM;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMMessageType;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.core.BmobIMClient;
import cn.bmob.newim.event.MessageEvent;
import cn.bmob.newim.event.OfflineMessageEvent;
import cn.bmob.newim.listener.BmobIMMessageHandler;
import cn.bmob.newim.notification.BmobNotificationManager;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**消息接收器
 * @author smile
 * @project DemoMessageHandler
 * @date 2016-03-08-17:37
 */
@SuppressWarnings("ALL")
public class MessageHandler extends BmobIMMessageHandler{

    private Context context;

    public MessageHandler(Context context) {
        this.context = context;
    }

    @Override
    public void onMessageReceive(final MessageEvent event) {
        //当接收到服务器发来的消息时，此方法被调用
        excuteMessage(event);
    }

    @Override
    public void onOfflineReceive(final OfflineMessageEvent event) {
        //每次调用connect方法时会查询一次离线消息，如果有，此方法会被调用
        Map<String,List<MessageEvent>> map =event.getEventMap();
        //挨个检测下离线消息所属的用户的信息是否需要更新
        for (Map.Entry<String, List<MessageEvent>> entry : map.entrySet()) {
            List<MessageEvent> list =entry.getValue();
            int size = list.size();
            for(int i=0;i<size;i++){
                excuteMessage(list.get(i));
            }
        }
    }
    private boolean isBlack(String uid){
        try {
            return BlackUserManager.getInstance(context).getBlackUser(uid) != null;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 处理消息
     * @param event
     */
    private void excuteMessage(final MessageEvent event){
        //检测用户信息是否需要更新
        UserModel.getInstance().updateUserInfo(event, new UpdateCacheListener() {
            @Override
            public void done(BmobException e) {
                BmobIMMessage msg = event.getMessage();
                if (BmobIMMessageType.getMessageTypeValue(msg.getMsgType()) == 0) {//用户自定义的消息类型，其类型值均为0
                    processCustomMessage(msg, event.getFromUserInfo());
                } else {//SDK内部内部支持的消息类型
                    if (BmobNotificationManager.getInstance(context).isShowNotification()) {//如果需要显示通知栏，SDK提供以下两种显示方式：
                        Intent pendingIntent = new Intent(context, ChatActivity.class);
                        BmobIMUserInfo info=msg.getBmobIMUserInfo();
                        if (isBlack(info.getUserId())){
                            return;
                        }
                        BmobIMConversation c = BmobIM.getInstance().startPrivateConversation(info,false,null);
                        BmobIMConversation conversation = BmobIMConversation.obtain(BmobIMClient.getInstance(), c);
                        pendingIntent.putExtra(Config.KEY_CONVERSATION,conversation);
                        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                        //1、多个用户的多条消息合并成一条通知：有XX个联系人发来了XX条消息
                        BmobNotificationManager.getInstance(context).showNotification(event, pendingIntent);
                        context.sendBroadcast(new Intent(MainActivity.MSG_REFRESH_CODE));
                        //2、自定义通知消息：始终只有一条通知，新消息覆盖旧消息
//                        BmobIMUserInfo info =event.getFromUserInfo();
//                        //这里可以是应用图标，也可以将聊天头像转成bitmap
//                        Bitmap largetIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
//                        BmobNotificationManager.getInstance(context).showNotification(largetIcon,
//                                info.getName(),msg.getContent(),"您有一条新消息",pendingIntent);
                    } else {//直接发送消息事件
                        EventBus.getDefault().post(event);
                    }
                }
            }
        });
    }

    /**
     * 处理自定义消息类型
     * @param msg
     */
    private void processCustomMessage(BmobIMMessage msg,BmobIMUserInfo info){
        //自行处理自定义消息类型
        String type =msg.getMsgType();
        //发送页面刷新的广播
        EventBus.getDefault().post(new RefreshEvent());
        //处理消息
        if(type.equals("add")){//接收到的添加好友的请求
            NewFriend friend = AddFriendMessage.convert(msg);
            //本地好友请求表做下校验，本地没有的才允许显示通知栏--有可能离线消息会有些重复
            long id = NewFriendManager.getInstance(context).insertOrUpdateNewFriend(friend);
            if(id>0){
                showAddNotify(friend);
            }
        }else if(type.equals("agree")){//接收到的对方同意添加自己为好友,此时需要做的事情：1、添加对方为好友，2、显示通知
            AgreeAddFriendMessage agree = AgreeAddFriendMessage.convert(msg);
            addFriend(agree.getFromId());//添加消息的发送方为好友
            //这里应该也需要做下校验--来检测下是否已经同意过该好友请求，我这里省略了
            showAgreeNotify(info,agree);
        }else if(type.equals("comment")){
            NewComment comment=SendCommentMessage.convert(msg);
            comment.setStatus(Config.STATUS_VERIFY_NONE);
            long id = NewCommentManager.getInstance(context).insertOrUpdateNewComment(comment);
            if (id>0){
                showCommentNotify(comment,id);
                context.sendBroadcast(new Intent(MainActivity.MSG_REFRESH_CODE));
            }
        }
    }

    /**
     *  显示评论通知
     * @param comment
     * @param id 本地数据库中的主键id
     */
    private void showCommentNotify(NewComment comment,long id){
        Intent pendingIntent = new Intent(context, DynamicDetailActivity.class);
        pendingIntent.putExtra(Constants.DYNAMIC_ID,comment.getDynamicId());
        pendingIntent.putExtra(Constants.COMMENT_ID,id);
        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        //这里可以是应用图标，也可以将聊天头像转成bitmap
        Bitmap largetIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        if (comment.getType()==0){
            BmobNotificationManager.getInstance(context).showNotification(largetIcon,
                    comment.getName(), "回复了你的动态："+comment.getContent(), "回复了你的动态："+comment.getContent(), pendingIntent);
        }else{
            BmobNotificationManager.getInstance(context).showNotification(largetIcon,
                    comment.getName(), "回复了你的评论："+comment.getContent(), "回复了你的评论："+comment.getContent(), pendingIntent);
        }
    }

    /**
     * 显示对方添加自己为好友的通知
     * @param friend
     */
    private void showAddNotify(NewFriend friend){
        Intent pendingIntent = new Intent(context, MainActivity.class);
        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        //这里可以是应用图标，也可以将聊天头像转成bitmap
        Bitmap largetIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        BmobNotificationManager.getInstance(context).showNotification(largetIcon,
                friend.getName(), friend.getMsg(), friend.getName() + "请求添加你为朋友", pendingIntent);
    }

    /**
     * 显示对方同意添加自己为好友的通知
     * @param info
     * @param agree
     */
    private void showAgreeNotify(BmobIMUserInfo info,AgreeAddFriendMessage agree){
        Intent pendingIntent = new Intent(context, MainActivity.class);
        pendingIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        Bitmap largetIcon = Util.getBitmapFromUrl(info.getAvatar());
        BmobNotificationManager.getInstance(context).showNotification(largetIcon,
                info.getName(),agree.getMsg(),agree.getMsg(),pendingIntent);
    }

    /**
     * 添加对方为自己的好友
     * @param uid
     */
    private void addFriend(String uid){
        User user =new User();
        user.setObjectId(uid);
        UserModel.getInstance().agreeAddFriend(user, new SaveListener() {
            @Override
            public void onSuccess() {
                Log.i("bmob", "onSuccess");
            }

            @Override
            public void onFailure(int i, String s) {
                Log.i("bmob", "onFailure:"+s+"-"+i);
            }
        });
    }
}
